import { getAreaAll } from "../../api/contracts";
import { getHouseResDetail, uplateHouseRes } from "../../api/houseRes";
import config from '../../config/index'
import { models } from '../../enums/model'
Page({
  data: {
    id: '',
    house_name: '',
    house_rent: 0,
    house_use: '',
    house_floor: '',
    house_type: '',
    house_area: '',
    house_mode: '',
    house_fitment: '',
    house_peace: '',
    house_position: '',
    house_address: '',
    landload_id: '',
    landload_phone: '',
    pay_method: '',
    house_plot: '',
    spec_address: '',
    house_remark: '',
    config,
    room: 1,
    living: 1,
    toilet: 1,
    cash: 1,
    pay: 1,
    building: 1,
    No: 1,
    use_list: ['公寓', '小区', '别墅', '自建房'],
    position_label: '',
    showUse: false,
    showMode: false,
    showPeace: false,
    showPosition: false,
    fitment_list: ['普通装修', '精装修', '豪华装修'],
    mode_list: models.map((item: any) => {
      return {
        label: item.label,
        value: item.value
      }
    }),
    mode_label: '',
    longitude: null as null | number,
    latitude: null as null | number,
  },
  handleTypeChange() {
    this.setData({
      house_type: `${this.data.room}-${this.data.living}-${this.data.toilet}`
    })
  },
  handleMethodChange() {
    this.setData({
      pay_method: `${this.data.cash}-${this.data.pay}`
    })
  },
  handleSpecChange() {
    this.setData({
      spec_address: `${this.data.building}-${this.data.No}`
    })
  },
  handleShowMode() {
    this.setData({
      showMode: !this.data.showMode
    })
  },
  handleShowUse() {
    this.setData({
      showUse: !this.data.showUse
    })
  },
  handleShowPeace() {
    this.setData({
      showPeace: !this.data.showPeace
    })
  },
  handleShowPosition() {
    this.setData({
      showPosition: !this.data.showPosition
    })
  },
  onModeChange(event: any) {
    const { value } = event.detail
    this.setData({
      house_mode: value.value,
      mode_label: value.label
    })
    this.handleShowMode()
  },
  onPeaceChange(event: any) {
    const { value } = event.detail
    this.setData({
      house_peace: value
    })
    this.handleShowPeace()
  },
  onUseChange(event: any) {
    const { value } = event.detail
    this.setData({
      house_use: value
    })
    this.handleShowUse()
  },
  async getPeaceData() {
    const res = await getAreaAll()
    if (res.statusCode === config.SUCCESS_CODE) {
      this.setData({
        peace_list: res.data.map((item: any) => {
          return item.item_value
        }),
        house_peace: res.data[0].item_value
      })
    }
  },
  choooseAddress() {
    let that = this
    wx.chooseLocation({
      success: function (res: any) {
        if (res.errMsg === 'chooseLocation:ok') {
          that.setData({
            longitude: res.longitude,
            latitude: res.latitude,
            house_address: res.address,
            house_position: `${res.longitude},${res.latitude}`
          })
        }
      }
    })
  },
  async sub() {
    let data: HouseRes = {
      // id: this.data.id,
      house_rent: +this.data.house_rent,
      house_use: this.data.house_use,
      house_floor: this.data.house_floor,
      house_type: this.data.house_type,
      house_area: this.data.house_area,
      house_mode: this.data.house_mode as 'FULL' | 'MORE' | 'SHORT',
      house_peace: this.data.house_peace,
      house_position: this.data.house_position,
      house_address: this.data.house_address,
      landload_id: this.data.landload_id,
      landload_phone: this.data.landload_phone,
      pay_method: this.data.pay_method,
      house_plot: this.data.house_plot,
      spec_address: this.data.spec_address,
      house_remark: this.data.house_remark
    }
    const res = await uplateHouseRes(this.data.id, data)
    if (res.statusCode === config.SUCCESS_CODE) {
      wx.showToast({
        title: '修改成功',
        icon: 'none'
      })
      const timer = setTimeout(() => {
        wx.navigateBack()
        clearTimeout(timer)
      }, 1000)
    } else {
      wx.showToast({
        title: '修改失败',
        icon: 'none'
      })
    }
  },
  onLoad(e: any) {
    let id = e.id
    this.setData({ id })
    this.getPeaceData()
    // this.mapCTX = wx.createMapContext('map')
    this.initDetail()
  },
  async initDetail() {
    const res = await getHouseResDetail(this.data.id)
    if (res.statusCode === config.SUCCESS_CODE) {
      this.setData({
        ...res.data,
        room: res.data.house_type.split('-')[0],
        living: res.data.house_type.split('-')[1],
        toilet: res.data.house_type.split('-')[2],
        cash: res.data.pay_method.split('-')[0],
        pay: res.data.pay_method.split('-')[1],
        building: res.data.spec_address.split('-')[0],
        No: res.data.spec_address.split('-')[1],
        mode_label: models.find(item => item.value === res.data.house_mode)!.label,
        longitude: Number(res.data.house_position.split(',')[0]),
        latitude: Number(res.data.house_position.split(',')[1])
      })
    }
  }
})